package com.darren.archive.controller;


import com.darren.archive.bean.ResponseBean;
import com.darren.archive.config.AppConfig;
import com.darren.archive.model.SysParameter;
import com.darren.archive.service.ISysParameterService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


/**
 * @Description: 系统参数接口
 * @Author: 周金涛
 * @Date: 2019-09-19 12:38:41
 */
@Api(value = "系统参数接口",tags = "系统参数接口",description = "系统参数接口")
@RestController
@Slf4j
public class SysParameterController {

    @Resource
    private AppConfig appConfig;

    @Resource
    private ISysParameterService sysParameterService;

    /**
     * 根据配置分类名、参数名、参数状态，获取系统参数
     * @param lookupCat        配置分类名
     * @param lookupType       参数名
     * @param enable           参数状态，是否启用，非必填，true 启用，false 禁用
     * @param pageNum          分页查询的页号
     * @param pageSize         分页查询的页大小
     * @return  月度社保公积金缴纳信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lookupCat",value = "部门ID，非必填",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "lookupType",value = "项目ID，非必填",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "enable",value = "参数状态，是否启用，非必填，true 启用，false 禁用",paramType = "query",dataType = "Boolean"),
            @ApiImplicitParam(name = "pageNum",value = "分页查询的页号,非必填",paramType = "query",dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize",value = "分页查询的页大小,非必填",paramType = "query",dataType = "Integer")
    })
    @ApiOperation(value = "根据配置分类名、参数名、参数状态，获取系统参数",notes = "根据配置分类名、参数名、参数状态，获取系统参数")
    @GetMapping("/sys/parameter/list")
    public ResponseBean getBusInsurance(@RequestParam(name = "lookupCat",required = false) String lookupCat,
                                        @RequestParam(name = "lookupType",required = false) String lookupType,
                                        @RequestParam(name = "enable",required = false) Boolean enable,
                                        @RequestParam(name = "pageNum",required = false) Integer pageNum,
                                        @RequestParam(name = "pageSize",required = false) Integer pageSize){
        pageNum = (pageNum == null ||  pageNum < 1) ? 1 : pageNum;
        pageSize = (pageSize == null ||  pageSize < 1) ? appConfig.getPageSize() : pageSize;
        PageHelper.startPage(pageNum,pageSize);
        return ResponseBean.isOk(PageInfo.of(sysParameterService.findByCondition(lookupCat,lookupType,enable)));
    }


    /**
     * 保存系统参数，如果 id 为空，则新增；id 不为空，则修改
     * @return   系统参数
     */
    @ApiOperation(value = "保存系统参数",notes = "保存系统参数 record，如果 id 为空，则新增；id 不为空，则修改")
    @PostMapping(value = "/sys/parameter/save",consumes = "application/json;charset=UTF-8")
    public ResponseBean save(@RequestBody @ApiParam(name="系统参数",value="传入json格式",required=true) SysParameter record){
        record = sysParameterService.save(record);
        if(record == null){
            return ResponseBean.isError("保存系统参数失败！");
        }
        return ResponseBean.isOk(record);
    }


    /**
     * 根据参数ID，获取系统参数
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "系统参数id，必填",paramType = "path",dataType = "Long")
    })
    @ApiOperation(value = "参数ID查询",notes = "系统参数ID作为URL的一部分，如：http://localhost/sys/parameter/{id}")
    @GetMapping("/sys/parameter/{id}")
    public ResponseBean getProjectsById(@PathVariable(name = "id") Long id){
        return ResponseBean.isOk(sysParameterService.findByPrimaryKey(id));
    }

}
